package com.niit.agricstore.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Notice;
import com.niit.agricstore.service.NoticeService;
import com.niit.agricstore.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @PostMapping("/create")
    public Result createNotice(@RequestBody Notice notice) {
        System.out.println(notice);
        int rows = noticeService.createNotice(notice);
        if( rows>0){
            return Result.success(rows);
        }
        return Result.error("407","插入失败");
    }

    @PutMapping("/update/{noticeId}")
    public Result<Void> updateNotice(@PathVariable int noticeId, @RequestBody Notice newNotice) {
        int rowsAffected = noticeService.updateNotice(noticeId, newNotice);
        if (rowsAffected > 0) {
            return Result.success();
        } else {
            return Result.error("202","Failed to update notice");
        }
    }

    @DeleteMapping("/delete/{noticeId}")
    public Result<Void> deleteNotice(@PathVariable int noticeId) {
        int rowsAffected = noticeService.deleteNotice(noticeId);
        if (rowsAffected > 0) {
            return Result.success();
        } else {
            return Result.error("202","Notice not found");
        }
    }

    @DeleteMapping("/deleteAll")
    public Result<Void> deleteAllNotices() {
        int rowsAffected = noticeService.deleteNoticeAll();
        if (rowsAffected > 0) {
            return Result.success();
        } else {
            return Result.error("202","Failed to delete all notices");
        }
    }

    @GetMapping("/all")
    public Result getAllNotices() {
        List<Notice> notices = noticeService.getAllNotices();
        return Result.success(notices);
    }

    @GetMapping("/myNotice")
    public Result getNoticesById(@RequestParam int noticeWho,@RequestParam int page, @RequestParam int pageSize) {
        PageInfo<Notice> noticePage = noticeService.getNoticesByUid(noticeWho,page,pageSize);
        if (noticePage != null) {
            return Result.success(noticePage);
        } else {
            return Result.error("202","Notice not found");
        }
    }

    @GetMapping("/type/{noticeType}")
    public Result<List<Notice>> getNoticesByType(@PathVariable int noticeType) {
        List<Notice> notices = noticeService.getNoticesByType(noticeType);
        return Result.success(notices);
    }

    @GetMapping("/page")
    public Result getNoticesByPage(@RequestParam int page, @RequestParam int pageSize) {
        Page<Notice> noticePage = noticeService.getNoticesByPage(page, pageSize);
        return Result.success(noticePage);
    }

    @PatchMapping("/status/{noticeId}/{status}")
    public Result<Void> updateNoticeStatus(@PathVariable int noticeId, @PathVariable int status) {
        noticeService.updateNoticeStatus(noticeId, status);
        return Result.success();
    }

    @GetMapping("/noticeByPage/{pageNum}/{pageSize}")
    public Result getNoticeByPage(@PathVariable("pageNum") String pageNum,
                                  @PathVariable("pageSize") String pageSize) {
        int pageNumi = Integer.valueOf(pageNum);
        int pageSizei = Integer.valueOf(pageSize);
        PageInfo<Notice> noticeList = noticeService.findNoticeAllByPage(pageNumi, pageSizei);
        return Result.success(noticeList);
    }
}